﻿using DevExpress.XtraEditors;
using MDK.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmOperationNoCodeTube : BaseForm
    {
        public int _Position { get; set; }

        private int index = 0;
        public frmOperationNoCodeTube()
        {
            InitializeComponent();
        }

        public frmOperationNoCodeTube(int Position) :this()
        {
            _Position = Position;
            InitTubeStatus();
            splashScreenManager1.ShowWaitForm();
            if (App.flow.BeginManaulHandle())
            {
                splashScreenManager1.CloseWaitForm();
            }
        }

        /// <summary>
        /// 初始化试管状态
        /// </summary>
        public void InitTubeStatus()
        {
            var pallet = App.flow.PosModel.DesktopPositions[_Position].Pallet;
            if (pallet != null)
            {
                var tubes = pallet.Tubes;
                //工位
                for (int i = 0; i < tubes.Count(); i++)
                {
                    if (tubes[i] != null)
                    {
                        if (string.IsNullOrEmpty(tubes[i].Barcode))
                        {
                            var control = ((PictureEdit)(this.Controls.Find("Tube" + i, true)[0]));
                            control.Image = MDK.Winform.Properties.Resources.无条码;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 试管按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Tube_Click(object sender, EventArgs e)
        {
            var controlname = (sender as PictureEdit).Name;
            int.TryParse(controlname.Substring(4, controlname.Length -4 ), out index);
            tubecode.Text = App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes[index].Barcode;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tubecode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes[index].Barcode = tubecode.Text;
                var control = ((PictureEdit)(this.Controls.Find("Tube" + index, true)[0]));
                control.Image = MDK.Winform.Properties.Resources.正常;
            }
        }


        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmOperationNoCodeTube_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (App.flow.PosModel.DesktopPositions[_Position].Pallet.NoBarcodeCount == 0)
            {
                App.flow.EndManaulHandle();
                e.Cancel = false;
            }
            else
            {
                FastMessageBox.Warnning("有无条码试管，请确认是否补完试管条码");
                e.Cancel = true;
            }
        }

       
    }
}
